﻿/**
 * PvZ Tool
 * 
 * @classname   Function
 * @description Custom functions for PvZ Tool
 * @author      Martomo
 * @author      Kamizoom
 * @version     0.0.2
 * 
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PvZTool
{
    public class Function
    {
        /**
         * Converts an HEX string to byte
         * 
         * @param hexString
         * @return byte
         */
        public static byte[] HexToData(string hexString)
        {
            if (hexString == null)
                return null;

            if (hexString.Length % 2 == 1)
                hexString = '0' + hexString; // Up to you whether to pad the first or last byte

            byte[] data = new byte[hexString.Length / 2];

            for (int i = 0; i < data.Length; i++)
                data[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);

            return data;
        }

        /**
         * Converts integer to a byte array
         * 
         * @param int32
         * @param reverse
         * @return byte
         */
        public static byte[] ConvertInt32ToByteArray(int int32, bool reverse = false)
        {
            byte[] buffer = BitConverter.GetBytes(int32);
            if (reverse == true) { Array.Reverse(buffer); }
            return buffer;
        }

        /**
         * Creates a message box
         * 
         * @param msgText
         * @param msgTitle
         * @return void
         */
        public static void MsgBox(string msgText, string msgTitle = "Error")
        {
            string boxTitle = Program.appName;
            if (msgTitle.Length > 0) { boxTitle += " - " + msgTitle;  }
            MessageBox.Show(msgText, boxTitle, MessageBoxButtons.OK);
        }
     }
}
